package com.example.otto.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.otto.entity.Manager;
import com.example.otto.entity.Task;
import com.example.otto.mapper.TaskMapper;
import com.example.otto.mapper.WorkerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class TaskService extends ServiceImpl<TaskMapper, Task> {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private WorkerMapper workerMapper;

    public List<Task> findByDate_Name(String name, Date date) {
        if (name != null && date != null){
           return taskMapper.findByDate_Name(name, date);
        }else if (name != null){
           return taskMapper.findByName(name);
        }else if (date != null){
           return taskMapper.findByDate(date);
        }else {
           return taskMapper.findAll();
        }
    }

    public boolean alterTask(Task task) {
        task.setWorkerPhone(taskMapper.findPhoneByWorkerId(task.getWorkerId()));
        return saveOrUpdate(task);
    }

    public boolean submitTask(Task task){
        task.setWorkerId(taskMapper.findDefaultWorker());
        task.setWorkerPhone(taskMapper.findPhoneByWorkerId(task.getWorkerId()));
        workerMapper.setBusy(task.getWorkerId());
        if(task.getWorkerId()!=null && task.getWorkerPhone()!=null)
            task.setState("1");
        return taskMapper.add(task);
    }

    public boolean confirmTask(Integer num){
        workerMapper.setFree(taskMapper.findHandlingWorkerId(num));
        return taskMapper.confirm(num);
    }

}
